import request from '../../utils/https.js';
import util from '../../utils/util.js';
var userId;
Page({

  /**
   * 页面的初始数据
   */

  data: {
    active: 0,
    pageNum: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  onClick(event) {
    const that = this;
    let navIndex = event.detail.index;
    let navName = event.detail.title;
    let navList = that.data.navList;
    console.log('素材Id===>', navList[navIndex].cateId)
    let cateId = navList[navIndex].cateId;
    that.setData({
      active: navIndex,
      cateId: cateId,
      pageNum: 1
    })
    that.orderList(cateId)
  },
  //素材列表
  orderList(cateId) {
    const that = this;
    request._get(getApp().Api_url + 'mate/cateInfo', {
      userId: userId,
      cateId: cateId,
      pageNum: that.data.pageNum,
      pageSize: 10
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        var list = res.data.data;
        that.setData({
          list: list.children
        })
      }

    }, function () {})

  },
  onDetails(e) {
    const that = this;
    let mateId = e.currentTarget.dataset.mateid;
    let kinds = e.currentTarget.dataset.kinds;
    wx.navigateTo({
      url: '/packageA/pages/mtDetails/mtDetails?mateId=' + mateId + '&kinds=' + kinds
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    userId = wx.getStorageSync("userId");
    const that = this;
    if (userId == '' || userId == null || userId == 'undefined') {
      // wx.reLaunch({
      //   url: '/pages/login/login?path=/pages/material/material'
      // })
      // return;
      userId = null
    }

    request._get(getApp().Api_url + 'mate/cate', { //banner
      userId: userId
    }, function (res) {
      if (res.data.errMsg == 'OK') {
        var navList = res.data.data;
        let cateId = navList[0].cateId;
        that.setData({
          active: 0,
          navList: navList,
          cateId: cateId
        })

        that.orderList(cateId)
      }

    }, function () {})

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    const that = this;
    that.setData({
      pageNum: that.data.pageNum + 1
    })

    request._get(getApp().Api_url + 'mate/cateInfo', {
      userId: userId,
      cateId: that.data.cateId,
      pageNum: that.data.pageNum,
      pageSize: 10
    }, function (res) {
      if (res.data.errMsg == 'OK') {

        if (!res.data.data.children || res.data.data.children.length <= 0 || res.data.data.children == null) {
          wx.showToast({
            icon: 'none',
            title: '已经到底了~~',
          })
        } else {
          let list = that.data.list;
          let newOrderList = res.data.data.children;
          that.setData({
            list: list.concat(newOrderList)
          })
        }
      }

    }, function () {})

  },
  //图片加载完成回调
   getLoad(e) {
      console.log(e.detail)
  }
})